﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.DAL;
using MINERP.MODEL.AutoOrder_Fuji;
using MINERP.BLL.Common;
using System.IO;

namespace MINERP.BLL.AutoOrder
{
    /// <summary>
    /// 从系统设置的路径拷贝文件到本地的图径
    /// </summary>
    public class DownLoadOrderFile_Fuji
    {
        private string _orderedFolderPath = string.Empty;//系统设定的已开单目录
        private string _watiOrderFolderPath = string.Empty;
        private const string ORDER_PERSON_NAME = "FUJI远程自动开单";

        /// <summary>
        /// 将远程目录下的文件移动到等待开单目录下,然后一边解析一边移动到已开单目录中
        /// </summary>
        /// <param name="downloadFolder"></param>
        /// <returns>0：正确;-1:在创建目标文件夹时出错:-2:移动文件夹时出错;-3:解析时出错</returns>
        public int DownloadFolderFromRemoteFolder(AutoOrderFolder_Info downloadFolder, out string msg, out string strDisFolder)
        {

            strDisFolder = GetMovedPath(downloadFolder);//目标目录

            msg = string.Empty;

            if (!Directory.Exists(strDisFolder))
            {
                try
                {
                    Directory.CreateDirectory(strDisFolder);
                }
                catch (Exception ex)
                {
                    msg = "创建文件夹是出错：" + ex.Message + "\n" + strDisFolder;
                    return -1;
                }
            }
            if (FileBLL.MoveDirectory(downloadFolder.FolderPath, strDisFolder, out msg))//复制到等待开单成功
            {
                strDisFolder += "\\" + downloadFolder.FolderName;
            }
            else//移动失败
            {
                return -2;
            }
            return 0;
        }

        #region help methods
        /// <summary>
        /// 组合将要移动到的目录,主要:等待开单目录\FUJI自动开单\IPS Order number\1.jpg...order.xml
        /// </summary>
        /// <returns></returns>
        private string GetMovedPath(AutoOrderFolder_Info downloadFolder)
        {
            if (string.IsNullOrEmpty(_watiOrderFolderPath))
            {
                _watiOrderFolderPath = FileDirPathBLL.GetWaitOrderFolder();
            }

            //return _watiOrderFolderPath + "\\" + ORDER_PERSON_NAME + "\\" + downloadFolder.FolderName;   
            return _watiOrderFolderPath + "\\" + ORDER_PERSON_NAME;
        }
        #endregion
    }
}
